<?php

namespace app\common\business;

use app\common\model\mysql\SPartnersCategory as SPartnersCategoryModel;
use app\common\model\mysql\SPartners as SPartnersModel;

class SPartners
{
    public $partnersCategoryObj = null;
    public $partnersObj = null;
    public function __construct()
    {
        $this->partnersCategoryObj = new SPartnersCategoryModel();
        $this->partnersObj = new SPartnersModel();
    }

    /**
     * 行业列表查询
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalPartnersCategory($where,$field,$limit,$page){
        $dictData = $this->partnersCategoryObj->getNormalPartnersCategoryList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    /**
     * 行业列表查询(无分页)
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalPartnersCategoryNotPage($where,$field){
        $dictData = $this->partnersCategoryObj->getNormalPartnersCategoryNotPageList($where,$field);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    //添加行业类别
    public function addPartnersCategory($data){
        try {
            //根据$config_name去数据库查询是否存在这条记录
            $postNameResult = $this->getPartnersCategoryByName($data['partners_category_name']);
            if($postNameResult){
                throw new \think\Exception("该行业类别名称已经存在请重新设置");
            }
            $this->partnersCategoryObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->partnersCategoryObj->id;
    }

    public function getPartnersCategoryByName($partners_category_name,$partners_category_id = ''){
        $user = $this->partnersCategoryObj->getPartnersCategoryByNameModel($partners_category_name,$partners_category_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //行业类别修改
    public function updatePartnersCategoryById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $postNameResult = $this->getPartnersCategoryByName($data['partners_category_name'],$id);
        if($postNameResult){
            throw new \think\Exception("该行业类别名称已经存在请重新设置");
        }
        $where = [
            "partners_category_id"=>$id,
        ];
        return $this->partnersCategoryObj->where($where)->save($data);
    }

    //根据$ids数组，删除数据库记录delete
    public function delPartnersCategoryIds($ids){
        $ids = explode(',', $ids);
        //判断此分类下是否有下级
        $where = [
            ["a.partners_category_id", "in", $ids],
            ["a.del_flag", "=", '0'],
        ];
        $partnersData = $this->getNormalPartners($where,"a.*",1,1);
        if($partnersData['data']){
            $partnersCategoryData = $this->getPartnersCategoryById($partnersData['data'][0]['partners_category_id']);
            throw new \think\Exception("合作商已设置该行业类别[".$partnersCategoryData['partners_category_name']."]，请先修改合作商信息，再删除");
        }

        $result = $this->partnersCategoryObj->delPartnersCategoryIds($ids);
        return $result;
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getPartnersCategoryById($id){
        $user = $this->partnersCategoryObj->getPartnersCategoryIdData($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    /****************** 合作商管理 ******************************/
    /**
     * 合作商查询列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalPartners($where,$field,$limit,$page){
        $dictData = $this->partnersObj->getNormalPartnersList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    //添加合作商
    public function addPartners($data){
        try {
            //根据$config_name去数据库查询是否存在这条记录
            $postNameResult = $this->getPartnersByName($data['partners_name']);
            if($postNameResult){
                throw new \think\Exception("该合作商名称已经存在请重新设置");
            }
            $this->partnersObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->partnersObj->id;
    }

    public function getPartnersByName($partners_name,$partners_id = ''){
        $user = $this->partnersObj->getPartnersByNameModel($partners_name,$partners_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    /**
     * 根据主键id更新合作商状态
     * @param $id
     * @param $data
     * @return bool
     */
    public function changeStatusPartners($partnersId,$status){
        $partnersId = intval($partnersId);
        if(empty($partnersId)){
            return false;
        }
        return $this->partnersObj->changeStatusPartnersModel($partnersId,$status);
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getPartnersById($id){
        $user = $this->partnersObj->getPartnersIdData($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //合作商修改
    public function updatePartnersById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $postNameResult = $this->getPartnersByName($data['partners_name'],$id);
        if($postNameResult){
            throw new \think\Exception("该合作商名称已经存在请重新设置");
        }
        $where = [
            "partners_id"=>$id,
        ];
        return $this->partnersObj->where($where)->find()->save($data);
    }

    //根据$ids数组，删除数据库记录delete
    public function delPartnersIds($ids){
        $ids = explode(',', $ids);
        $result = $this->partnersObj->delPartnersIds($ids);
        return $result;
    }
}
